<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TicketsController
 *
 * @author Poh Nean
 */
class TicketsController extends PsController
{

	public $defaultAction = 'index';
	public $layout = 'manage';

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'roles' => array('ManageModule.Tickets'),
					),
						), parent::accessRules());
	}

	public function moduleAccessRules()
	{
		return array(
			array('Propsage.TicketManagement')
		);
	}

	public function actionIndex()
	{
		$agency = $this->loadAgency();


		$escalationLevel = new EscalationLevel;
		if (isset($_POST['EscalationLevel'])) {
			$escalationLevel->agency_id = Yii::app()->user->model->agency_id;
			$escalationLevel->level = $escalationLevel->getNextEscalationLevel();
			$escalationLevel->attributes = $_POST['EscalationLevel'];
			$isValid = $escalationLevel->validate();

			if ($isValid) {
				if ($escalationLevel->save(false)) {
					$this->refresh();
				}
			} else {
				PsSystemMessage::add('Error adding escalation level.', 'error');
			}
		}

		$criteria = new CDbCriteria();
		$criteria->compare('t.agency_id', $agency->id);

		$dataProvider = new CActiveDataProvider('EscalationLevel', array(
			'criteria' => $criteria
		));

		$data = array();
		$data['dataProvider'] = $dataProvider;
		$data['agency'] = $agency;
		$data['newEscalationLevel'] = $escalationLevel;
		$this->render('index', $data);
	}

	public function actionEscalationLevelUpdate($level)
	{
		$escalationLevel = $this->loadEscalationLevel($level);

		if (isset($_POST['EscalationLevel'])) {
			$escalationLevel->agency_id = Yii::app()->user->model->agency_id;
			$escalationLevel->attributes = $_POST['EscalationLevel'];
			$isValid = $escalationLevel->validate();

			if ($isValid) {
				if ($escalationLevel->save(false)) {
					$this->redirect(array('modules/tickets/index'));
				}
			} else {
				PsSystemMessage::add('Error updating escalation level.', 'error');
			}
		}

		$data = array();
		$data['escalationLevel'] = $escalationLevel;
		$this->render('escalation_level_update', $data);
	}

	public function actionEscalationLevelDelete($level)
	{
		$escalationLevel = $this->loadEscalationLevel($level);

		if (!$escalationLevel->delete()) {
			PsSystemMessage::add('Error deleting escalation level.', 'error');
		}
		
		if (!Yii::app()->request->isAjaxRequest) {
			$this->redirect(array('modules/tickets/index'));
		}
	}

	public function loadEscalationLevel($level)
	{
		$escalationLevel = EscalationLevel::model()->ofCurrentAgency()->findByAttributes(array('level' => $level));

		if ($escalationLevel === null) {
			throw new PsPageNotFoundException;
		}

		return $escalationLevel;
	}

	/**
	 * @return Agency the agency of the currently logged in user.
	 */
	public function loadAgency()
	{
		return Yii::app()->user->model->agency;
	}

	public function getUserListData()
	{
		$users = User::model()->ofCurrentAgency()->findAll(array('condition' => 't.status = ' . User::STATUS_ACTIVE, 'order' => 't.username ASC'));

		$listData = array();
		foreach ($users as $user) {
			$listData[$user->id] = '[' . $user->username . '] ' . $user->name;
		}

		return $listData;
	}

}

?>
